// ES6的解构赋值知识点
//(1)数组的解构赋值
let [x, y, z] = [1, 2, 3]  // a=1, b=2, c=3（模式匹配：左右结构一致）
console.log("[x, y, z] = ",[x, y, z])
console.log("x:", x, "y:", y, "z:", z,"\n")


let [foo, [[bar], baz]] = [1, [[2], 3]]  //嵌套数组解构：foo=1, bar=2, baz=3
console.log("[foo, [[bar], baz]] = ",[foo, [[bar], baz]])
console.log("foo:", foo, "bar:", bar, "baz:", baz,"\n")

let [, , third] = ["foo", "bar", "baz"]  //用逗号跳过元素：third="baz"（跳过前两个元素）
console.log("[, , third] = ",[, , third])
console.log("third:", third,"\n")


let [head, ...tail] = [1, 2, 3, 4]  //用“...”收集剩余元素为数组：head=1, tail=[2,3,4]
console.log("[head, ...tail] = ",[head, ...tail])
console.log("head:", head, "tail:", tail,"\n")


let [s, d] = [1] //解构不成功‌变量值为 undefined : x=1, y=undefined（右侧元素不足）
console.log("[s, d] = ",[s, d])
console.log("s:", s, "d:", d,"\n")



console.log("\n")

//(2)对象的解构赋值
let { name1, age1 } = { name1: "小红", age1: 15 }  // name="小红", age=15（变量名需与属性名一致）
console.log("{ name1, age1 } = ",{ name1, age1 })
console.log("name1:", name1, "age1:", age1,"\n")


let { name: username, age: userAge } = { name: "小红", age: 15 } //属性名重命名： username="小红", userAge=15（冒号左侧为属性名，右侧为变量名）
console.log("{ name: username, age: userAge } = ",{ name: username, age: userAge })
console.log("username:", username, "userAge:", userAge,"\n")


let { user: { userName } } = { user: { userName: "小明" } } //嵌套对象解构： userName="小明"
console.log("{ user: { userName } } = ",{ user: { userName } })
console.log("userName:", userName,"\n")